package com.example.yucheng.controller;

import com.example.yucheng.domain.ReceivingPrice;

import com.example.yucheng.domain.ReceivingUnit;
import com.example.yucheng.service.ReceivingPriceService;
import com.example.yucheng.service.ReceivingUnitService;
import com.example.yucheng.util.PageQuery;
import com.example.yucheng.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(("/receiving"))
@CrossOrigin
public class ReceivingUnitController {

    @Autowired
    ReceivingUnitService receivingUnitService;

    @Autowired
    ReceivingPriceService receivingService;
    @GetMapping("/getReceivingList")
    public Result getReceivingUnit(@RequestParam(required = false) Integer pageNum,@RequestParam(required = false) Integer pageSize){
        List<ReceivingUnit> receivingList = receivingUnitService.getReceivingList();
        if (pageNum==null){
            return Result.success(receivingUnitService.getReceivingList());
        }else{
            List<ReceivingUnit> receivingListByLimit = receivingUnitService.getReceivingListByLimit(new PageQuery((pageNum - 1)*pageSize, pageSize, 1));
            Map re = new HashMap();
            re.put("ReceivingList",receivingListByLimit);
            re.put("total",receivingList.size());
            return Result.success(re);
        }
    }


    @DeleteMapping("/deleteReceiving")
    public Result<String> deleteReceivingUnit(@RequestParam int[] ids){
        boolean deleteReceving = false;
        for (Integer temp:ids){
            deleteReceving   = receivingUnitService.deleteReceivingPrice(temp);
        }
        if (deleteReceving){
            return Result.success("删除成功");
        }else{
            return Result.success("删除失败");
        }


    }

    @PutMapping("/updateReceiving")
    public Result updateReceivingUnit(@RequestBody ReceivingUnit receiving){
        List<ReceivingUnit> receivingList = receivingUnitService.getReceivingList();
        boolean c =true;
        for (ReceivingUnit r:receivingList){
            if (r.getName().equals(receiving.getName())){
                c=false;
            }
        }
        if (c){
            boolean b = receivingUnitService.updateReceivingPrice(receiving);
            return Result.success(b);
        }else {
            return Result.success("已存在");
        }

    }

    @PostMapping("/addReceiving")
    public Result insertReceivingUnit(@RequestBody ReceivingUnit receiving){
        List<ReceivingUnit> receivingList = receivingUnitService.getReceivingList();
        boolean c =true;
        for (ReceivingUnit r:receivingList){
            if (r.getName().equals(receiving.getName())){
                c=false;
            }
        }
        if (c){
            boolean b = receivingUnitService.insertReceivingPrice(receiving);
            return Result.success(b);
        }else{
            return Result.success("已存在该用户");
        }
    }



    @GetMapping("/getReceivingCostPriceList")
    public Result getReceiving(@RequestParam(required = false) Integer pageNum,@RequestParam(required = false) Integer pageSize){
        List<ReceivingPrice> receivingList = receivingService.getReceivingList();
        if (pageNum==null){
            return Result.success(receivingService.getReceivingList());
        }else{
            List<ReceivingPrice> receivingListByLimit = receivingService.getReceivingListByLimit(new PageQuery((pageNum - 1)*pageSize, pageSize, 1));
            Map re = new HashMap();
            re.put("ReceivingList",receivingListByLimit);
            re.put("total",receivingList.size());
            return Result.success(re);
        }
    }

    @PostMapping("/getReceivingCostPrice")
    public Result getReceiving(@RequestBody ReceivingPrice receiving){
        System.out.println(receiving);
        int costPrice=0;
        try{
            costPrice= receivingService.getCostPrice(receiving);
        }catch (Exception e){
            return Result.success("没有该对应单价");
        }
        Map re = new HashMap();
        re.put("costPrice",costPrice);
        return Result.success(re);

    }


    @DeleteMapping("/deleteReceivingCostPrice")
    public Result<String> deleteReceivingPrice(@RequestParam int[] ids){
        boolean deleteReceving = false;
        for (Integer temp:ids){
            deleteReceving   = receivingService.deleteReceivingPrice(temp);
        }
        if (deleteReceving){
            return Result.success("删除成功");
        }else{
            return Result.success("删除失败");
        }


    }

    @PutMapping("/updateReceivingCostPrice")
    public Result updateReceivingPrice(@RequestBody ReceivingPrice receiving){
        boolean b = receivingService.updateReceivingPrice(receiving);
        return Result.success(b);
    }

    @PostMapping("/addReceivingGoodPrice")
    public Result insertReceivingPrice(@RequestBody ReceivingPrice receiving){
        System.out.println(receiving);
        boolean b = receivingService.insertReceivingPrice(receiving);
        return Result.success(b);
    }


}
